package com.allwees.bs.c.module.payment.controller;

import com.allwees.bs.c.module.base.controller.BaseController;
import com.allwees.bs.c.module.order.dto.OrderDTO;
import com.allwees.bs.c.module.order.mq.event.PaypalCaptureEvent;
import com.allwees.bs.c.module.order.service.CartService;
import com.allwees.bs.c.module.payment.req.PaymentCaptureReq;
import com.allwees.bs.c.module.payment.req.PaymentPayPalAppCancelReq;
import com.allwees.bs.c.module.payment.req.PaymentPayPalCheckoutReq;
import com.allwees.bs.c.module.payment.service.IPaymentService;
import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.core.common.constant.EPlatform;
import com.allwees.core.common.util.http.RequestContext;
import com.allwees.core.mq.rabbitmq.MqUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * paypal支付相关
 */
@Slf4j
@RestController
public class PaymentController extends BaseController {

    @Autowired
    private IPaymentService paymentService;

    @Autowired
    private CartService cartService;

    //*************************************************paypal*********************************************************//

    /**
     * 创建paypal支付
     * @param req
     * @return
     */
    @PostMapping("/api/v1/payment/paypal/create")
    public R createPaypalPayment(@RequestBody @Validated PaymentPayPalCheckoutReq req) {
        try {
            return R.ok(paymentService.createPaypalWeb(req));
        }catch(Exception e){
            log.error("", e);
            return R.error("order creation failed");
        }
    }

    /**
     * 取消支付
     * @param req
     * @return
     */
    @PostMapping("/api/v1/payment/paypal/cancel")
    public R cancelPayment(@RequestBody @Validated PaymentPayPalAppCancelReq req) {
        paymentService.cancelPayment(req);
        return R.ok();
    }


    /**
     * paypal支付捕获
     *
     * @param req
     * @return
     */
    @PostMapping("/api/v1/payment/paypal/capture")
    public R paypaylCapture(@RequestBody PaymentCaptureReq req) {
        //mq
        MqUtil.send(new PaypalCaptureEvent(req));
        cartService.clearUserCart(getUserUuid());
        OrderDTO order = this.paymentService.capture(req);
        return R.ok(order);
    }

    /**
     * paypal支付捕获
     * @param req
     * @return
     */
    @Deprecated
    @PostMapping("/api/v1/payment/app/paypal/capture")
    public R createAppPayment(@RequestBody @Validated PaymentCaptureReq req) {
        OrderDTO dto = paymentService.appCapture(req);
        return R.ok(dto);
    }

    /**
     * paypal回调 支付成功
     * @return
     */
    @Deprecated
    @GetMapping("/api/v1/payment/app/paypal/return")
    public ModelAndView paypaylAppReturn(@RequestParam(value = "token") String paymentId,
                                         @RequestParam(value = "PayerID") String payerId,
                                         @RequestParam(value = "orderUuid") String orderUuid,
                                         @RequestParam(value = "platform") String platform,
                                         HttpServletRequest httpServletRequest) {
        log.info(" paypaylAppReturn >>>>>>>>>>>>>>>>>>>>>>>>>>>> paymentId:{},payerId:{},orderUuid:{},platform:{}", paymentId, payerId, orderUuid, platform);

        PaymentCaptureReq captureReq = new PaymentCaptureReq(paymentId,payerId,orderUuid,EPlatform.parseFrom(platform), RequestContext.getChannel());
        OrderDTO order = this.paymentService.capture(captureReq);
        return _appView(captureReq.getPlatform(), "succeed", orderUuid);
    }

    /**
     * paypal回调 取消支付
     * @param orderUuid
     * @param platform
     * @return
     */
    @Deprecated
    @GetMapping("/api/v1/payment/app/paypal/cancel")
    public ModelAndView paypalCancel(@RequestParam(value = "orderUuid") String orderUuid,
                          @RequestParam(value = "platform") String platform) {
        log.info(" paypalCancel >>>>>>>>>>>>>>>>>>>>>>>>>>>> orderUuid:{},platform:{}", orderUuid, platform);
        return _appView(EPlatform.parseFrom(platform), "cancelled", orderUuid);
    }

    private ModelAndView _appView(EPlatform platform, String templateNameSuffix, String orderUuid) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("orderUuid", orderUuid);
        mv.setViewName(_getAppWebViewTemplate(platform, templateNameSuffix));
        return mv;
    }

    private String _getAppWebViewTemplate(EPlatform platform, String templateNameSuffix) {
		return platform.name().toLowerCase() + "_payment_" + templateNameSuffix;
    }
}
